﻿using ECNow.Core;
using ECNow.Core.Data;
using ECNow.Data.DataInterfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ECNow.Data.Mysql
{
    public class ProductDac : DataAccessBase<Product, int>, IProductDac
    {
        public int Delete(int id)
        {
            StringBuilder sbSql = new StringBuilder();
            sbSql.Append(@"update [dbo].[Product]
                           set isdeleted=1
                           where Id = @SysId");

            return ExecuteNoQuery(sbSql.ToString(), new { SysId = id});
        }

        public Product Get(int id)
        {
            StringBuilder sbSql = new StringBuilder();
            sbSql.Append(@"SELECT `ecnow_product`.`Id`,
                                `ecnow_product`.`Deleted`,
                                `ecnow_product`.`InDate`,
                                `ecnow_product`.`InUser`,
                                `ecnow_product`.`EditDate`,
                                `ecnow_product`.`EditUser`,
                                `ecnow_product`.`Name`,
                                `ecnow_product`.`BrifeName`,
                                `ecnow_product`.`Description`,
                                `ecnow_product`.`ProductTemplateId`,
                                `ecnow_product`.`VendorId`,
                                `ecnow_product`.`MetaKeywords`,
                                `ecnow_product`.`MetaDescription`,
                                `ecnow_product`.`MetaTitle`,
                                `ecnow_product`.`Sku`,
                                `ecnow_product`.`ManufacturerPartNumber`,
                                `ecnow_product`.`Gtin`,
                                `ecnow_product`.`IsShipEnabled`,
                                `ecnow_product`.`IsFreeShipping`,
                                `ecnow_product`.`ShippingCharge`,
                                `ecnow_product`.`Price`,
                                `ecnow_product`.`OldPrice`,
                                `ecnow_product`.`ProductCost`,
                                `ecnow_product`.`Weight`,
                                `ecnow_product`.`Length`,
                                `ecnow_product`.`Width`,
                                `ecnow_product`.`Height`,
                                `ecnow_product`.`DisplayOrder`,
                                `ecnow_product`.`Published`
                            FROM `ecdb`.`ecnow_product`
                           where Id = @SysId");
            return Get<Product>(sbSql.ToString(), new { SysId = id });
        }

        public Product GetBySku(string sku)
        {
            throw new NotImplementedException();
        }

        public List<Product> GetList()
        {
            StringBuilder sbSql = new StringBuilder();
            sbSql.Append(@"SELECT `ecnow_product`.`Id`,
                                `ecnow_product`.`Deleted`,
                                `ecnow_product`.`InDate`,
                                `ecnow_product`.`InUser`,
                                `ecnow_product`.`EditDate`,
                                `ecnow_product`.`EditUser`,
                                `ecnow_product`.`Name`,
                                `ecnow_product`.`BrifeName`,
                                `ecnow_product`.`Description`,
                                `ecnow_product`.`ProductTemplateId`,
                                `ecnow_product`.`VendorId`,
                                `ecnow_product`.`MetaKeywords`,
                                `ecnow_product`.`MetaDescription`,
                                `ecnow_product`.`MetaTitle`,
                                `ecnow_product`.`Sku`,
                                `ecnow_product`.`ManufacturerPartNumber`,
                                `ecnow_product`.`Gtin`,
                                `ecnow_product`.`IsShipEnabled`,
                                `ecnow_product`.`IsFreeShipping`,
                                `ecnow_product`.`ShippingCharge`,
                                `ecnow_product`.`Price`,
                                `ecnow_product`.`OldPrice`,
                                `ecnow_product`.`ProductCost`,
                                `ecnow_product`.`Weight`,
                                `ecnow_product`.`Length`,
                                `ecnow_product`.`Width`,
                                `ecnow_product`.`Height`,
                                `ecnow_product`.`DisplayOrder`,
                                `ecnow_product`.`Published`
                            FROM `ecdb`.`ecnow_product`");
            return GetList<Product>(sbSql.ToString()).ToList();
        }

        public Page<Product> GetList(int pageindex, int pagesize)
        {
            StringBuilder totalSql = new StringBuilder();
            totalSql.Append(@"SELECT count(*)
                            FROM `ecdb`.`ecnow_product`");
            var sumCount = Get<Int32>(totalSql.ToString());

            StringBuilder sbSql = new StringBuilder();
            sbSql.Append(@"SELECT `ecnow_product`.`Id`,
                                `ecnow_product`.`Deleted`,
                                `ecnow_product`.`InDate`,
                                `ecnow_product`.`InUser`,
                                `ecnow_product`.`EditDate`,
                                `ecnow_product`.`EditUser`,
                                `ecnow_product`.`Name`,
                                `ecnow_product`.`BrifeName`,
                                `ecnow_product`.`Description`,
                                `ecnow_product`.`ProductTemplateId`,
                                `ecnow_product`.`VendorId`,
                                `ecnow_product`.`MetaKeywords`,
                                `ecnow_product`.`MetaDescription`,
                                `ecnow_product`.`MetaTitle`,
                                `ecnow_product`.`Sku`,
                                `ecnow_product`.`ManufacturerPartNumber`,
                                `ecnow_product`.`Gtin`,
                                `ecnow_product`.`IsShipEnabled`,
                                `ecnow_product`.`IsFreeShipping`,
                                `ecnow_product`.`ShippingCharge`,
                                `ecnow_product`.`Price`,
                                `ecnow_product`.`OldPrice`,
                                `ecnow_product`.`ProductCost`,
                                `ecnow_product`.`Weight`,
                                `ecnow_product`.`Length`,
                                `ecnow_product`.`Width`,
                                `ecnow_product`.`Height`,
                                `ecnow_product`.`DisplayOrder`,
                                `ecnow_product`.`Published`
                            FROM `ecdb`.`ecnow_product`");

            sbSql.Append(string.Format("LIMIT {0}, {1}", pageindex, pagesize));

            Page<Product> result = new Page<Product>();
            result.Records = GetList<Product>(sbSql.ToString()).ToList();
            result.Paging = new Paging();
            result.Paging.Total = sumCount;

            return result;
        }

        public string GetSku(int id)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 插入商品到数据库
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Insert(Product entity)
        {
            StringBuilder sbSql = new StringBuilder();

            sbSql.Append(
            @"INSERT INTO `ecdb`.`ecnow_product`
                    (`Deleted`,
                    `InDate`,
                    `InUser`,
                    `EditDate`,
                    `EditUser`,
                    `Name`,
                    `BrifeName`,
                    `Description`,
                    `ProductTemplateId`,
                    `VendorId`,
                    `MetaKeywords`,
                    `MetaDescription`,
                    `MetaTitle`,
                    `Sku`,
                    `ManufacturerPartNumber`,
                    `Gtin`,
                    `IsShipEnabled`,
                    `IsFreeShipping`,
                    `ShippingCharge`,
                    `Price`,
                    `OldPrice`,
                    `ProductCost`,
                    `Weight`,
                    `Length`,
                    `Width`,
                    `Height`,
                    `DisplayOrder`,
                    `Published`)
                VALUES
                    (@Deleted,
                    @InDate,
                    @InUser,
                    @EditDate,
                    @EditUser,
                    @Name,
                    @BrifeName,
                    @Description,
                    @ProductTemplateId,
                    @VendorId,
                    @MetaKeywords,
                    @MetaDescription,
                    @MetaTitle,
                    @Sku,
                    @ManufacturerPartNumber,
                    @Gtin,
                    @IsShipEnabled,
                    @IsFreeShiping,
                    @ShippingCharge,
                    @Price,
                    @OldPrice,
                    @ProductCost,
                    @Weight,
                    @Length,
                    @Width,
                    @Height,
                    @DisplayOrder,
                    @Published)");

            return ExecuteNoQuery(sbSql.ToString(), new
            {
                Deleted = entity.Deleted,
                InDate = DateTime.Now,
                InUser = entity.InUser,
                EditDate = DateTime.Now,
                EditUser = entity.EditUser,
                Name = entity.Name,
                BrifeName = entity.BrifeName,
                Description = entity.Description,
                ProductTemplateId = entity.ProductTemplateId,
                VendorId = entity.VendorId,
                MetaKeywords = entity.MetaKeywords,
                MetaDescription = entity.MetaDescription,
                MetaTitle = entity.MetaTitle,
                Sku = entity.Sku,
                ManufacturerPartNumber = entity.ManufacturerPartNumber,
                Gtin = entity.Gtin,
                IsShipEnabled = entity.IsShipEnabled,
                IsFreeShiping = entity.IsFreeShipping,
                ShippingCharge = entity.ShippingCharge,
                Price = entity.Price,
                OldPrice = entity.OldPrice,
                ProductCost = entity.ProductCost,
                Weight = entity.Weight,
                Length = entity.Length,
                Width = entity.Width,
                Height = entity.Height,
                DisplayOrder = entity.DisplayOrder,
                Published = entity.Published
            });
        }

        public int Update(Product entity)
        {
            throw new NotImplementedException();
        }

        public int UpdateStatus(int status)
        {
            throw new NotImplementedException();
        }
    }
}
